package com.app.beu.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class SerializeHelper {
		
	public static byte readByte(ByteArrayInputStream buffer) {
		return (byte) buffer.read();
	}

	public static short readShort(ByteArrayInputStream buffer) {
		short res = 0;
		for (int i = 0; i < 2; i++) {
			res <<= 8;
			res |= (0xFF & buffer.read());
		}
		return res;
	}

	public static byte[] readBytes(ByteArrayInputStream buffer) {
		int len = readInt(buffer);
		if (len < 0) {
			return null;
		}
		byte[] result = new byte[len];
		read(result, buffer);
		return result;
	}

	public  static long readLong(ByteArrayInputStream buffer) {
		long res = 0;
		for (int i = 0; i < 8; i++) {
			res <<= 8;
			res |= (0xFF & buffer.read());
		}
		return res;
	}

	public  static int readInt(ByteArrayInputStream buffer) {
		int res = 0;
		for (int i = 0; i < 4; i++) {
			res <<= 8;
			res |= (0xFF & buffer.read());
		}
		return res;
	}

	public  static boolean readBoolean(ByteArrayInputStream buffer) {
		return (buffer.read() == 1);
	}

	public static char readChar(ByteArrayInputStream buffer) {
		short s = readShort(buffer);
		return (char) s;
	}

	public static String readString(ByteArrayInputStream buffer) {
		int length = readInt(buffer);
		if (length == -1) {
			return null;
		}
		byte[] data = new byte[length * 2];
		read(data, buffer);
		char[] str = new char[length];
		int offset;
		for (int i = 0; i < length; i++) {
			offset = 2 * i;
			short ch = (short) (data[offset] & 0xFF);
			ch <<= 8;
			ch |= (short) (data[offset + 1] & 0xFF);
			str[i] = (char) ch;
		}
		return new String(str);
	}

	private static void _writeInt(int b, ByteArrayOutputStream buffer) {
		for (int i = 3; i >= 0; i--) {
			buffer.write((byte) (b >> i * 8));
		}
	}

	public  static void write(byte[] bytes, ByteArrayOutputStream buffer) {
		if (bytes == null) {
			_writeInt(-1, buffer);
		} else {
			int len = bytes.length;
			_writeInt(len, buffer);
			buffer.write(bytes, 0, len);
		}
	}

	public static void write(byte[] bytes, int offset, int len, ByteArrayOutputStream buffer) {
		_writeInt(len, buffer);
		buffer.write(bytes, offset, len);
	}

	public static void writeByte(byte b, ByteArrayOutputStream buffer) {
		buffer.write(b);
	}

	public static void writeBoolean(boolean b, ByteArrayOutputStream buffer) {
		if (b) {
			buffer.write((byte) 1);
		} else {
			buffer.write((byte) 0);
		}
	}

	public static void writeInt(int b, ByteArrayOutputStream buffer) {
		for (int i = 3; i >= 0; i--) {
			buffer.write((byte) (b >> i * 8));
		}
	}

	public static void writeShort(short j, ByteArrayOutputStream buffer) {
		for (int i = 1; i >= 0; i--) {
			buffer.write((byte) (j >> i * 8));
		}
	}

	public static void writeLong(long l, ByteArrayOutputStream buffer) {
		for (int i = 7; i >= 0; i--) {
			buffer.write((byte) (l >> i * 8));
		}
	}

	public static void writeChar(char c, ByteArrayOutputStream buffer) {
		short t = (short) c;
		writeShort(t, buffer);
	}

	public static void writeString(String s, ByteArrayOutputStream buffer) {
		if (s == null)
		{
			s = "";
		}
		int length = s.length();
		byte[] data = new byte[4 + 2 * length];
		for (int i = 3; i >= 0; i--) {
			data[3 - i] = (byte) (length >> i * 8);
		}
		int offset;
		for (int i = 0; i < length; i++) {
			short ch = (short) s.charAt(i);
			offset = i * 2;
			data[4 + offset] = (byte) (ch >> 8);
			data[5 + offset] = (byte) ch;
		}
		buffer.write(data, 0, data.length);
	}
	
	public static byte[] getBytes(long l) {
		byte[] result = new byte[8];
		for (int i = 7; i >= 0; i--) {
			result[7 - i] = (byte) (l >> i * 8);
		}
		return result;
	}
	
	public static long getLong(byte[] value) {
		long res = 0;
		for (int i = 0; i < 8; i++) {
			res <<= 8;
			res |= (0xFF & value[i]);
		}
		return res;
	}
	
	private static void read(byte[] buffer, ByteArrayInputStream stream) {
		int offset = 0;
		int read;
		while (offset < buffer.length && (read = stream.read(buffer, offset, buffer.length - offset)) > -1) {
			offset += read;
		}
	}
}
